package com.chatsimple.netty

import com.chatsimple.logDebug
import com.ly.data.MessageProtobuf.TaskProto
import io.netty.buffer.ByteBuf
import io.netty.channel.ChannelHandlerContext
import io.netty.handler.codec.ByteToMessageDecoder

class TaskDecoder : ByteToMessageDecoder() {
    override fun decode(ch: ChannelHandlerContext?, byteBuf: ByteBuf?, output: MutableList<Any>?) {
        ch ?: return
        byteBuf ?: return
        output ?: return
        val contentLength = byteBuf.readIntLE()
        logDebug("客户端(${ch.channel().remoteAddress()}) 发送内容字节长度:$contentLength")
        val contentArray = ByteArray(contentLength)
        byteBuf.readBytes(contentArray)
        try {
            val task = TaskProto.parseFrom(contentArray)
            output.add(task)
        } catch (e: Exception) {
            logDebug("客户端(${ch.channel().remoteAddress()}) proto解析错误:$e")
        }
    }
}